package com.huizhonglm.diap.ui.app;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.bruce.common.utils.EmptyUtil;
import com.huizhonglm.diap.R;
import com.huizhonglm.diap.api.model.app.CheckUpdateResponse;
import com.huizhonglm.diap.core.upgrade.Upgrade;
import com.huizhonglm.diap.core.utils.DeviceUtil;
import com.huizhonglm.diap.core.utils.DialogUtil;
import com.huizhonglm.diap.core.utils.ToastUtil;
import com.huizhonglm.diap.core.utils.UserUtil;
import com.huizhonglm.diap.presenters.app.CheckUpdatePersenter;
import com.huizhonglm.diap.ui.BaseActivity;
import com.huizhonglm.diap.ui.app.iView.CheckUpdateIView;
import com.huizhonglm.diap.ui.tab.TabhostActivity;
import com.huizhonglm.diap.ui.user.PasswdModifyActivity;

/**
 * 描述当前类的作用
 *
 * @author LiXiaoLong
 * @version 1.0
 * @since 2016-03-03 10:14
 */
public class SettingActivity extends BaseActivity implements CheckUpdateIView, View.OnClickListener {

    private CheckUpdatePersenter cuPersenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        setBackEvent(this, null);
        cuPersenter = new CheckUpdatePersenter(this);
        assignViews();
    }

    @Override
    protected void assignViews() {

        View modifyPwd = findViewById(R.id.modify_pwd_txt);
        View service = findViewById(R.id.service_layout);
        View checkUpdate = findViewById(R.id.check_update_txt);
        View feedback = findViewById(R.id.feedback_txt);
        Button logoutBtn = (Button) findViewById(R.id.logout_btn);
        TextView versionTxt = (TextView) findViewById(R.id.version_txt);

        versionTxt.setText("惠众联盟  V " + DeviceUtil.getVersionName());

        if (UserUtil.isLogin()) {
            logoutBtn.setVisibility(View.VISIBLE);
        } else {
            logoutBtn.setVisibility(View.GONE);
        }

        modifyPwd.setOnClickListener(this);
        service.setOnClickListener(this);
        checkUpdate.setOnClickListener(this);
        feedback.setOnClickListener(this);
        logoutBtn.setOnClickListener(this);
    }

    @Override
    public void onCheckVersionSuccess(CheckUpdateResponse response) {
        hideProgressBar();
        if (response != null) {
            showUpdateDialog(response);
        } else {
            ToastUtil.makeTextShort("已是最新版本");
        }
    }

    @Override
    public void onCheckVersionFailure() {
        hideProgressBar();
        ToastUtil.makeTextShort("检测新版本失败，请稍后重试");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.modify_pwd_txt:
                startActivity(new Intent(this, PasswdModifyActivity.class));
                break;
            case R.id.service_layout:
                DeviceUtil.makePhoneCall(this, "02989381718");
                break;
            case R.id.check_update_txt:
                showProgressBar();
                cuPersenter.checkUpdate();
                break;
            case R.id.feedback_txt:
                startActivity(new Intent(this, FeedbackActivity.class));
                break;
            case R.id.logout_btn:
                UserUtil.logout();
                startActivity(new Intent(this, TabhostActivity.class));
                finish();
                break;
        }
    }

    private void showUpdateDialog(final CheckUpdateResponse resp){
        if (!EmptyUtil.isEmpty(resp.getApkUrl())) {
            DialogUtil.showNormalDialog(this, "已有新版本" + resp.getVersion(), resp.getDesc(), "暂不更新", "立即更新", new DialogUtil.OnDialogBtnClickListener() {
                @Override
                public void onBtnClick(String btnText) {
                    if ("立即更新".equals(btnText)) {
                        Upgrade.doUpgrade(SettingActivity.this, resp.getMode(), resp.getApkUrl(), resp.getVersion());
                    }
                }
            });
        } else {
            ToastUtil.makeTextShort("已是最新版本");
        }
    }
}
